//重新渲染表单函数
function renderForm() {
    layui.use('form', function() {
        var form = layui.form;
        form.render();
    });
}

//重新渲染表单函数
function alertOpen(title,content) {
    layui.use('layer', function() {
        layer.open({
            title: title
            ,content: content
        });
    });
}


//查询地址公共方法
function getAddress(regionalPId,name,choice){

    $.ajax({
        url: path + '/pc/region/getRegionList',
        type: 'get',
        data: {regionalPId: regionalPId},
        headers: {
            'X-USER-TOKEN':localStorage.getItem("token")
        },
        success: function (data) {
            if (data.code != 0) {
                layer.open({
                    title: '提示信息'
                    , content: data.message
                });
            } else {
                $("#"+name).html("");
                var defaultValue = "";
                if(name == "customerProvinces"){
                    defaultValue = "请选择省份";
                }else if(name == "customerCities"){
                    defaultValue = "请选择城市";
                }else if(name == "customerCounties"){
                    defaultValue = "请选择区域";
                }
                var html = "<option value=''>"+defaultValue+"</option>";
                for (var i = 0; i < data.data.length; i++) {
                    if(!isNull(choice) && data.data[i].regionalId == choice){
                        console.log(data.data[i].regionalName);
                        html += '<option selected value="' + data.data[i].regionalId + '">' + data.data[i].regionalName + '</option>';
                    }else{
                        html += '<option value="' + data.data[i].regionalId + '">' + data.data[i].regionalName + '</option>';
                    }
                }
                $("#"+name).append(html);
                renderForm();
            }
        },
        traditional: true
    });
}

//获取用户列表
function getUserList(name,choice){
    $.ajax({
        url: path+'/pc/user/getUserInfoListAll',
        type: 'get',
        headers: {
            'X-USER-TOKEN':localStorage.getItem("token")
        },
        // contentType : 'application/json',
        // dataType: 'json',
        data: {},
        success:function(data){
            if(data.code != 0){
                layer.open({
                    title: '提示信息'
                    ,content: data.message
                });
            }else{
                var html = "<option value=''>请选择</option>";
                $("#"+name).html("");
                for(var i=0;i<data.data.length;i++){
                    console.log(choice+"    "+data.data[i].userBaseInfoId);
                    console.log(choice == data.data[i].userBaseInfoId);
                    console.log(data.data[i].regionalName);
                    if(!isNull(choice) && data.data[i].userBaseInfoId == choice){
                        html += "<option selected value='"+data.data[i].userBaseInfoId+"'>"+data.data[i].nickName+"</option>";
                    }else{
                        html += "<option value='"+data.data[i].userBaseInfoId+"'>"+data.data[i].nickName+"</option>";
                    }
                }
                $("#"+name).html(html);
                renderForm();
            }
        }
    })
}


//获取项目时间
function getProTime(proId,proType){
    $.ajax({
        url: path+'/pc/project/getProjectDateInfo',
        type: 'get',
        headers: {
            'X-USER-TOKEN':localStorage.getItem("token")
        },
        // contentType : 'application/json',
        // dataType: 'json',
        data: {"idProAdmin":proId,"StageType":proType},
        success:function(data){
            if(data.code != 0){
                layer.open({
                    title: '提示信息'
                    ,content: data.message
                });
            }else{
                $("#beginTime").val(data.data.startDate);
                $("#endTime").val(data.data.endDate);
                $("#differenceTime").val(data.data.haveTime);

            }
        }
    })
}



//图片文件
function updateFile(ele,imgName,inputName){
    layui.use('upload', function(){
        var upload = layui.upload;//执行实例
        var uploadInst = upload.render({
            elem: '#'+ele //绑定元素
            ,url: path+'/pc/upload/uploadFile' //上传接口
            ,accept: 'file' //允许上传的文件类型
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    var suffixName = file.name.substring(file.name.lastIndexOf("."),file.name.length);
                    $('#'+imgName).attr('src', getIos(suffixName,result)); //图片链接（base64）
                });
            }
            ,done: function(res){
                //上传完毕回调
                //$("#imgUrl").attr("src",res.data.filePath);
                $("#"+inputName).val(res.data.idSysFiles);
            }
            ,error: function(){
                //请求异常回调
            }
            ,progress: function(n){
                var percent = n + '%' //获取进度百分比
                element.progress('demo', percent); //可配合 layui 进度条元素使用
            }
        });
    });
}

//富文本图片上传
function getLayedit(name){
    var contentIndex;
    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: path + '/pc/upload/uploadFile' //接口url
                ,type: 'post' //默认post
            }
        });
        return contentIndex = layedit.build(name); //建立编辑器
    });
}


//ajax请求封装
function ajaxFun(method,url,params,callback){
    if(isNull(localStorage.getItem("token"))){
        localStorage.removeItem("token");
        parent.window.location.href = path+"/login.htm";
        return false;
    }
    $.ajax({
        url: path+url,
        type: method,
        data: params,
        headers: {
            'X-USER-TOKEN':localStorage.getItem("token")
        },
        success:function(data){
            callback(data);
        },
        error: function (jqXHR) {
            alertFun(jqXHR.responseJSON);
        }

    })
}

function ajaxFunJson(method,url,params,callback){
    if(isNull(localStorage.getItem("token"))){
        localStorage.removeItem("token");
        parent.window.location.href = path+"/login.htm";
        return false;
    }
    $.ajax({
        url: path+url,
        type: method,
        data: params,
        contentType: "application/json",//比较关键
        headers: {
            'X-USER-TOKEN':localStorage.getItem("token")
        },
        success:function(data){
            callback(data);
        },
        error: function (jqXHR) {
            alertFun(jqXHR.responseJSON);
        }
    })
}

//错误请求处理
function alertFun(data){
    alert(data.message);
    if(data.code == 400){
        localStorage.removeItem("token");
        parent.window.location.href = path+"/login.htm";
    }
}

//统一判断请求
function isToken(){
    if(isNull(localStorage.getItem("token"))){
        localStorage.removeItem("token");
        parent.window.location.href = path+"/login.htm";
        return false;
    }
}

